/**
 * @param {number[]} piles
 * @param {number} h
 * @return {number}
 */
export var minEatingSpeed = function (piles, h) {
  let [left, right] = [1, Math.max(...piles)]
  let ans = right
  while (left <= right) {
    const mid = (left + right) >> 1
    // 当速度为k时，吃完所有的所须时间为sum
    const sum = piles.reduce((acc, cur) => acc + Math.ceil(cur / mid), 0)
    if (sum > h) {
      // sum > h, 说明这个速度吃不完香蕉
      left = mid + 1
    } else {
      ans = mid
      right = mid - 1
    }
  }
  return ans
}
